import { useMount } from "ahooks";
import React, { useState } from "react";
import { useHistory, useLocation, useModel } from "umi";
const LoginGuard = (props: any) => {
    const { userToken, login } = useModel('useAuthModel')
    const [isLogin, setIsLogin] = useState<boolean>(false)
    const history = useHistory();
    const authenticationLocation = useLocation() as any;
    console.log(authenticationLocation)
    useMount(async () => {
        const code = authenticationLocation.query?.code
        if (userToken) {
            setIsLogin(true)
            return;
        }
        if (!code) {
            history.push('/login')
            return;
        }
        await login(code)
        history.push('/welcome')
        setIsLogin(true)
    })
    console.log('进入了守卫')
    if (isLogin) {
        return (
            <>{props.children}</>
        )
    }
    else {
        return <></>
    }
}
export default LoginGuard